package com.example.home.findWork.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.findWork.bean.WordHotBean;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class WordHotAdapter extends RecyclerView.Adapter<WordHotAdapter.MyHoldy> {
    private Context context;
    private List<WordHotBean.RowsBean> list;

    public WordHotAdapter(Context context, List<WordHotBean.RowsBean> list) {
        // 根据工资对列表进行排序
        Collections.sort(list, new Comparator<WordHotBean.RowsBean>() {
            @Override
            public int compare(WordHotBean.RowsBean o1, WordHotBean.RowsBean o2) {
                // 假设salary是一个字符串;对于数值比较，使用Integer.parseInt()。
                return o1.getSalary().compareTo(o2.getSalary());
            }
        });

        // 如果列表包含3个以上的元素，则只保留前3个
        if (list.size() > 3) {
            this.list = list.subList(0, 3);
        } else {
            this.list = list;
        }

        this.context = context;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_work, null);
        return new WordHotAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        holder.name.setText(list.get(position).getName());
        holder.salary.setText(list.get(position).getSalary());
        holder.address.setText(list.get(position).getAddress());
        holder.need.setText(list.get(position).getNeed());
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView salary;
        private TextView address;
        private TextView need;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            salary = itemView.findViewById(R.id.salary);
            address = itemView.findViewById(R.id.address);
            need = itemView.findViewById(R.id.need);
        }
    }
}
